//
//  YTSelectCarTypeViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/7/8.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTSelectCarTypeViewController.h"
#import "YTSelecteCarTypeViewModel.h"
#import "YTSelectCarTypeViewCell.h"
#import "YTCarBandTypeModel.h"
#import <YYWebImage/YYWebImage.h>

@interface YTSelectCarTypeViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *bandTableView;
@property (weak, nonatomic) IBOutlet UITableView *versionTableView;
@property (weak, nonatomic) IBOutlet UITableView *colorTableView;
@property (strong, nonatomic) YTSelecteCarTypeViewModel *viewModel;
@end

@implementation YTSelectCarTypeViewController

- (void)initialize {
    [YTLoadingView show];
    _viewModel = [[YTSelecteCarTypeViewModel alloc] init];
    _bandTableView.tableFooterView = [UIView new];
    [self createBorder:_bandTableView];
    _versionTableView.tableFooterView = [UIView new];
    [self createBorder:_versionTableView];
    _colorTableView.tableFooterView = [UIView new];
    [self createBorder:_colorTableView];
    @weakify(self);
    [_viewModel.loadBandList subscribeNext:^(id x) {
        @strongify(self);
        [YTLoadingView dismiss];
        [self.bandTableView reloadData];
    }];
}

- (void)createBorder:(UITableView *)tableView {
    tableView.layer.borderColor = [UIColor grayColor].CGColor;
    tableView.layer.borderWidth = 0.5f;
}

#pragma mark - UITableViewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:_colorTableView]) { // color
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YTSeleteCarColorCell" forIndexPath:indexPath];
        cell.textLabel.text = _viewModel.colorArray[indexPath.row].name;
        UIColor *color = [UIColor yt_colorWithHexString:_viewModel.colorArray[indexPath.row].value];
        cell.imageView.image = [UIImage yy_imageWithColor:color size:CGSizeMake(30, 30)];
        cell.imageView.layer.cornerRadius = 15;
        cell.imageView.layer.borderWidth = 1.0f;
        cell.imageView.layer.masksToBounds = YES;
        cell.imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        if (_viewModel.colorArray[indexPath.row].isSelected) {
            cell.textLabel.textColor = kMainBlueColor;
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        return cell;
    }
    
    // 其他两个公用cell
    BOOL version = [tableView isEqual:_versionTableView];
    BOOL band = [tableView isEqual:_bandTableView];
    NSString *reuseIdentifier = version ? @"YTSeleteCarVersionCell" : @"YTSeleteCarTypeCell";
    YTSelectCarTypeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.selected = _viewModel.bandArray[indexPath.row].isSelected;
    if (band) {
        cell.textLabel.textColor = _viewModel.bandArray[indexPath.row].isSelected ? kMainBlueColor : [UIColor blackColor];
        cell.textLabel.text = _viewModel.bandArray[indexPath.row].name;
    }
    if (version) {
        cell.textLabel.textColor = _viewModel.versionArray[indexPath.row].isSelected ? kMainBlueColor : [UIColor blackColor];
        cell.textLabel.text = _viewModel.versionArray[indexPath.row].name;
    }
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([tableView isEqual:_versionTableView]) {
        return _viewModel.versionArray.count;
    }
    
    if ([tableView isEqual:_colorTableView]) {
        return _viewModel.colorArray.count;
    }
    
    return _viewModel.bandArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:_colorTableView]) { // 颜色
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        _viewModel.colorArray[indexPath.row].isSelected = YES;
        [self colorCell:cell isSelected:YES];
        return;
    }
    
    YTSelectCarTypeViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.textColor = kMainBlueColor;
    if ([tableView isEqual:_bandTableView] && _viewModel.bandArray[indexPath.row].versionList.count) {
        _versionTableView.hidden = NO;
        _viewModel.bandArray[indexPath.row].isSelected = YES;
        _viewModel.versionArray = _viewModel.bandArray[indexPath.row].versionList;
        [_versionTableView reloadData];
        return;
    }
    
    if ([tableView isEqual:_versionTableView]) {
        _colorTableView.hidden = NO;
        _viewModel.versionArray[indexPath.row].isSelected = YES;
        for (YTBandColorModel *color in _viewModel.colorArray) {
            color.isSelected = NO;
        }
        [_colorTableView reloadData];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:_colorTableView]) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        _viewModel.colorArray[indexPath.row].isSelected = NO;
        [self colorCell:cell isSelected:NO];
        return;
    }
    YTSelectCarTypeViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([tableView isEqual:_bandTableView]) {
        _viewModel.bandArray[indexPath.row].isSelected = NO;
    } else {
        _viewModel.versionArray[indexPath.row].isSelected = NO;
    }
    cell.textLabel.textColor = [UIColor blackColor];
}

#pragma mark - 选择完成

- (IBAction)completionAction:(UIBarButtonItem *)sender {
    if (_callBack) {
        [_callBack sendNext:RACTuplePack(_viewModel.bandArray[_bandTableView.indexPathForSelectedRow.row],
                                         _viewModel.versionArray[_versionTableView.indexPathForSelectedRow.row],
                                         _viewModel.colorArray[_colorTableView.indexPathForSelectedRow.row])];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - Private

- (void)colorCell:(UITableViewCell *)cell isSelected:(BOOL)selected {
    cell.textLabel.textColor = selected ? kMainBlueColor : [UIColor blackColor];
    cell.accessoryType = selected ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
}

@end
